<template>
    <view class="pub_page pub_page_bg pub_index pub_header_padding pub_tabbar_padding">
        <pub-head :headerConfig="headerConfig"></pub-head>
        <pub-tabbar :active="0" :adSrc="adSrc"></pub-tabbar>
        <div class="pub_page_scroll page_box">
            <div class="page_content">
                <div class="pub_banner">
                    <swiper class="swiper" circular :indicator-dots="true" :autoplay="true" :interval="5000" v-cloak:indicator-dots="false">
                        <swiper-item v-for="(item, key) in bannerList" :key="key">
                            <view class="swiper_item" :style="{ backgroundImage: 'url(' + item + ')' }"></view>
                        </swiper-item>
                    </swiper>
                </div>
                <div class="notice_div" @click="jumpPage({url:'/pages/other/document',data:{id:noticeInfo.noticeId}})">
                    <div class="title">
                        <div class="gg_txt"></div>
                        <text class="icon icon-font-basic icon-b-arrow-1-2"></text>
                    </div>
                    <div class="txt">
                        <uni-notice-bar v-if="noticeInfo.noticeDescribe" scrollable :speed="10" color="#565656" :text="noticeInfo.noticeDescribe" />
                    </div>
                </div>
                <div class="tabs_div">
                    <div class="lis">
                        <div class="li" v-for="(li,key) in crystalSortList" :key="key" @click="jumpPage({url:'/pages/crystalGoods/list',data:{crystalCategoryId:li.id}})">
                            <pub-image class="img" :src="li.img"></pub-image>
                            <div class="txt">{{ li.name }}</div>
                        </div>
                    </div>
                </div>
                <div class="zb_div" @click="$comFun.toast('敬请期待')"></div>
                <div class="ping_div" v-if="pingList.length">
                    <div class="title" @click="jumpPage({url:'/pages/ping/list'})">
                        <text class="icon icon-font-basic icon-b-arrow-1-2"></text>
                    </div>
                    <div class="lis_box">
                        <PingList :list="pingList" type='small' />
                    </div>
                </div>
                <div class="crystal_goods_div">
                    <div class="title" @click="jumpPage({url:'/pages/ping/list'})">今日推荐</div>
                    <div class="lis_box">
                        <crystalGoodsList :list="crystalGoodsList" />
                    </div>
                    <div v-if="crystalGoodsList.length === 0 && loadFinish" class="pub_nodata_1">空空如也~~</div>
                </div>
            </div>
        </div>
    </view>
</template>

<script>
import PingList from '@/pages/ping/components/pingList.vue';
import crystalGoodsList from '@/pages/crystalGoods/components/goodList.vue';
export default {
    components: { PingList, crystalGoodsList },
    data() {
        return {
            headerConfig: {
                title: '拼团',//标题栏内容
                header_color: 'rgba(0,0,0,0)'
            },

            bannerIndex: 0,
            bannerList: [],
            noticeInfo: {
                noticeDescribe: "",
                noticeId: "",
            },
            adSrc: '',

            pingList: [],
            crystalGoodsList: [],
            loadFinish: false,

            crystalSortList: this.$constant.crystalCategory,
        };
    },
    mounted() {
        this.getBanner();
        this.getNoticeInfo();
        this.getPingList()
        this.getCrystalGoodsList()
        // // this.getPopAd();
        // this.getUserInfo();
    },
    methods: {
        async getPopAd() {
            const res = await this.$request.get('public/getAd', {
                key: 'open-ad'
            });
            // console.log(res);
            if (res.code == 200) {
                this.adSrc = res.result.length > 0 && res.result[0].image ? res.result[0].image : '';
            }
        },
        async getBanner() {
            const res = await this.$request.get('public/getAd', {
                key: 'index-ad'
            });
            // console.log(res);
            if (res.code == 200 && res.result.length > 0) {
                this.bannerList = res?.result?.map(li => li?.image).filter(li => li);
            }
        },
        bannerChange(event) {
            this.bannerIndex = event.detail.current
        },
        async getNoticeInfo() {
            const res = await this.$request.get('public/getLastNotice', {});
            // console.log(res);
            if (res.code == 200 && res.result) {
                this.noticeInfo = res.result;
            }
        },
        async getPingList() {
            const res = await this.$request.get('goods/getGoodsPage', {
                pageNo: 1,
                pageSize: 3,
                isRecom: 1,
            });
            // console.log(res);
            if (res.code == 200 && res.result) {
                this.pingList = res.result.records.map(li => {
                    return {
                        ...li,
                        pro: (li.stock / li.maxNum).toFixed(2) * 100,
                    }
                })
            }
        },
        async getCrystalGoodsList() {
            const res = await this.$request.get('goods/getCrystalRecommendGoods', {});
            // console.log(res);
            this.loadFinish = true
            if (res.code == 200) {
                this.crystalGoodsList = res.result?.list || [];
            }
        },



        async getUserInfo() {
            const res = await this.$request.get('user/userInfo', {});
            // console.log(res);
            if (res.code == 200) {
                this.userInfo = res.result;
                if (!this.userInfo.province) {
                    // #ifdef APP-PLUS
                    this.getUserLocation();
                    // #endif
                    // #ifdef H5
                    this.locationInfo = {
                        latitude: 30.513494,
                        longitude: 104.060066,
                        address_txt: '四川省 成都市 双流区',
                        address: {
                            country: "中国",
                            province: "四川省",
                            city: "成都市",
                            district: "双流区",
                            street: "安公路",
                            streetNum: "3段-422",
                            cityCode: "75"
                        },
                    }
                    // this.saveUserLocation();
                    // #endif
                }
            }
        },
        async getUserLocation() {
            const that = this;
            // result 1=>已获取授权  0=>未获取授权-1=>被永久拒绝授权
            const result = await this.$permision.requestAndroidPermission('android.permission.ACCESS_FINE_LOCATION');
            console.log("result:", result);
            if (result == 1) {
                uni.getLocation({
                    type: 'gcj02',
                    geocode: true,
                    success: function (res) {
                        console.log(res);
                        that.locationInfo = {
                            latitude: res.latitude,
                            longitude: res.longitude,
                            address_txt: `${res.address.province} ${res.address.city} ${res.address.district}`,
                            address: res.address,
                        };
                        that.saveUserLocation();
                    }
                });
            } else {
                // this.dialogConfig = {
                //     title: '提示',//标题栏内容
                //     content: '请先获取定位信息再进入APP',
                //     btn_list: [{
                //         txt: '已设置',
                //         color: '#555',
                //         bg_1: '#e8e8e8',
                //         bg_2: '#eee',
                //     }, {
                //         txt: '去设置权限',
                //         color: '#fff',
                //         bg_1: '#31A5FF',
                //         bg_2: '#0974FF'
                //     }]
                // };
                // this.dialogShow = true;
            }
        },
        async saveUserLocation() {
            uni.setStorageSync('userLocationInfo', this.locationInfo);
            const res = await this.$request.post('user/bindPosition', {
                lat: this.locationInfo.latitude,
                lon: this.locationInfo.longitude,
            });
            console.log(res);
        },
    },
};
</script>

<style lang="scss" scoped>
@import "@/common/css/index.scss";
</style>
